package com.aisile.article.service.impl;

import cn.hutool.core.util.IdUtil;
import com.aisile.article.mapper.ApArticleConfigMapper;
import com.aisile.article.mapper.ApArticleContentMapper;
import com.aisile.article.service.ArticleInfoService;
import com.aisile.common.exception.CustomExceptionCatch;
import com.aisile.model.article.dtos.ArticleInfoDto;
import com.aisile.model.article.pojos.ApArticleConfig;
import com.aisile.model.article.pojos.ApArticleContent;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther:yry
 * @Date:2024/1/13 0013
 * @VERSON:1.0
 */
@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        if(dto == null){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 定义返回的数据结构  vo  map
        Map<String,Object> resultMap = new HashMap<>();
        // 查询文章的各种配置信息
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(
                Wrappers.<ApArticleConfig>lambdaQuery().eq(
                        ApArticleConfig::getArticleId,dto.getArticleId()
                )
        );
        if(apArticleConfig == null || apArticleConfig.getIsDelete() || apArticleConfig.getIsDown()){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 查询文章内容
        ApArticleContent apArticleContent =  apArticleContentMapper.selectOne(
                Wrappers.<ApArticleContent>lambdaQuery().eq(
                        ApArticleContent::getArticleId,dto.getArticleId()
                )
        );
        if(apArticleContent == null){
            CustomExceptionCatch.catchApp(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 封装map
        resultMap.put("config",apArticleConfig);
        resultMap.put("content",apArticleContent);
        return ResponseResult.okResult(resultMap);
    }
}
